﻿package com.sakura;


import java.io.FileInputStream;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class sendEmailTest2 {
	public static void main(String[] args) throws Exception{
		
		Properties props=new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.163.com");//设置smtp服务器
		Session session=Session.getInstance(props,
				new Authenticator(){													//此处使用的是一种模式，叫策略模式。把一段能实现具体功能的代码
					protected PasswordAuthentication getPasswordAuthentication()		//封装到一个对象里面，调用对象的函数即等于调用了那一段代码
					{
						return new PasswordAuthentication("13072368672@163.com","maozhi2764147");//邮箱帐号和密码
					}
				}
		);
		session.setDebug(true);
		
		Message msg=new MimeMessage(session);
		//发送邮箱的帐号
		msg.setFrom(new InternetAddress("13072368672@163.com"));
		msg.setSubject("这是多人html格式邮件测试喔！");
		//设置收件人    参数说明 1.收件人格式TO是正常收件人 BCC暗送 CC抄送 2.收件人邮箱 此处是群发
		msg.setRecipients(RecipientType.TO, InternetAddress.parse("1184225309@qq.com,13072368672@163.com"));
		msg.setContent("<span style='color:green'>绿色</span>", "text/html;charset=gbk");//参数说明 1.html文本内容 2.解析格式
		//msg.setContent("<span style='color:red'>中文呵呵呵</span>", "text/html;charset=gbk");   //记得不要写错charset
		Transport.send(msg);
		
//		Message msg = new MimeMessage(session,new FileInputStream("resource\\demo3.eml"));
//		Transport.send(msg,InternetAddress.parse("10000@qq.com"));
	}
}
